/*
 * custommessageimgdialog 带图片的提示框
 */
import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.14
import QtGraphicalEffects 1.14

Item {
    id: root

    // 私有属性
    property int _radius: 13
    // 提示框信息
    property var _boxMessage: undefined  // 实际上是JSON字符串
    //                                      boxMessage {
    //                                         title: string,
    //                                         text: string,
    //                                         standardButton: StandardButton(enum)
    //                                      }
    // 提示框ICON
    property url _iconPath: ""
    // 提示框文字
    property string _text: "--BoxMsg Default--"
    // 提示框背景颜色
    property string _startBgColor: "#007BD8"
    property string _endBgColor: "#001A5A"
    // 提示框图片路径
    property url _imgPath: "qrc:/icons/quchu_haocai.gif"
    // 按钮渲染
    property int _buttonNumber: 0 // 按位或得来的

    // 函数
    // 解析回传信息
    function analyseBoxMessage(boxMsg){
        if(boxMsg.length === 0){
            _text = "提示信息有误，请检查！"
        }else {
            console.log(boxMsg)
            var json = JSON.parse(boxMsg)
            _text = json["text"] // 提示框文字
            _buttonNumber = json["standardButton"]
        }
    }

    // 按钮处理
    function analyseButtonNumber(btnNum){
        if(btnNum === GLOBAL.StandardButton.NoButton){
            console.log("No Button")
        }

        if(btnNum & GLOBAL.StandardButton.Cancel){
            console.log("Cancel Button")
            createButton("MessageBoxButton.qml",
                         btnLayout,
                         {
                             "Layout.preferredWidth": 126,
                             "Layout.preferredHeight": 42,
                             "msgBtnWidth": 126,
                             "msgBtnHeight": 42,
                             "msgBtnText": "取消",
                             "msgBtnRadius": 8,
                             "msgBtnBorderColor": "transparent"
                         })
        }

        if(btnNum & GLOBAL.StandardButton.Submit){
            console.log("Submit Button")
            createButton("MessageBoxButton.qml",
                         btnLayout,
                         {
                             "Layout.preferredWidth": 126,
                             "Layout.preferredHeight": 42,
                             "msgBtnWidth": 126,
                             "msgBtnHeight": 42,
                             "msgBtnText": "确定",
                             "msgBtnColor": "#2F42CD",
                             "msgBtnRadius": 8,
                             "msgBtnBorderColor": "#999",
                             "msgBtnFontColor": "#FFF"
                         })
        }
    }

    // 按钮创建
    function createButton(qmlFileName, parent, propertyObj, eventList){
        var comp = Qt.createComponent(qmlFileName)
        if(comp.status === Component.Ready){
            var compObj = comp.createObject(parent, propertyObj)

        }
    }

    // c++信号的槽函数连接
    Connections {
        target: msgImgBox
        function onBoxMessageChanged(boxmsg){
            _boxMessage = boxmsg
            analyseBoxMessage(_boxMessage)
            analyseButtonNumber(_buttonNumber)
        }
    }
    /* property 定义结束 */

    Rectangle {
        id: background
        anchors.fill: parent

        LinearGradient {
            anchors.fill: parent
            start: Qt.point(0, 0)
            end: Qt.point(0, height)
            gradient: Gradient {
                GradientStop {position: 0.0; color: _startBgColor}
                GradientStop {position: 1.0; color: _endBgColor}
            }
        }

        Rectangle {
            id: container
            anchors {
                fill: parent
                margins: 17
            }
            radius: 13
            color: "#C8FFFFFF"

            Item {
                anchors.fill: parent

                ColumnLayout {
                    anchors.fill: parent

                    // 图片
                    AnimatedImage {
                        Layout.fillWidth: true
                        fillMode: Image.PreserveAspectFit
                        mipmap: true
                        smooth: true
                        source: _imgPath
                        Layout.alignment: Qt.AlignTop | Qt.AlignHCenter
                    }

                    // 提示文字
                    Label {
                        Layout.fillWidth: true
                        Layout.topMargin: 17
                        font.family: 'Alibaba PuHuiTi'
                        font.pixelSize: 19
                        font.bold: true
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        color: "#FF0000"
                        text: _text
                        Layout.alignment: Qt.AlignTop | Qt.AlignHCenter
                    }

                    // 按钮
                    Item {
                        Layout.fillWidth: true
                        Layout.topMargin: 17
                        Layout.bottomMargin: 17
                        Layout.alignment: Qt.AlignTop | Qt.AlignHCenter

                        RowLayout {
                            anchors.fill: parent
                            CustomHorSpacerItem {}

                            // 按钮居中对齐
                            RowLayout{
                                id: btnLayout
                                spacing: 8
                            }

                            CustomHorSpacerItem {}
                        }
                    }

                    CustomVerSpacerItem {}
                }
            }
        }
    }
}
